package net.jalbum.filters;

import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import se.datadosen.component.ControlPanel;
import se.datadosen.jalbum.Msg;

public class RedEyeControl extends MultipleResizableCircleControl implements FocusListener {
	/**
	 *
	 */
	private static final long serialVersionUID = 5684493925517473234L;
	protected RedEyeFilterPlugin redEyeFilterPlugin;
	protected JCheckBox showAdvanced;
	protected JTextField value1;
	protected JTextField value2;
	protected JTextField maxGap;
	protected ControlPanel advancedPanel;

	public RedEyeControl() {
	}

	public RedEyeControl(final RedEyeFilterPlugin redEyeFilterPlugin) {

		this.init(redEyeFilterPlugin);

		 addCircleControls();

		this.addMultipleControls();

		 addResizableControls();

		this.addAdvancedOptions();
	}

	protected void init(final RedEyeFilterPlugin redEyeFilterPlugin) {
		this.redEyeFilterPlugin = redEyeFilterPlugin;
		super.init(redEyeFilterPlugin);
	}

	protected void addAdvancedOptions() {
		this.removeAll();
		this.add(this.removeButton);
		this.removeButton.setEnabled(false);

		this.showAdvanced = new JCheckBox(Msg.getString(this, "filters.redEye.showAdvanced"), false);
		this.value1 = new JTextField(4);
		this.value2 = new JTextField(4);
		this.maxGap = new JTextField(4);
		this.advancedPanel = new ControlPanel();

		this.showAdvanced.addActionListener(this);
		this.value1.addActionListener(this);
		this.value2.addActionListener(this);
		this.maxGap.addActionListener(this);

		this.value1.addFocusListener(this);
		this.value2.addFocusListener(this);
		this.maxGap.addFocusListener(this);

		this.advancedPanel.setVisible(false);

		this.add("p", this.showAdvanced);

		this.advancedPanel.add("br", new JLabel(Msg.getString(this, "filters.redEye.value1")));
		this.advancedPanel.add("br", this.value1);
		this.advancedPanel.add("br", new JLabel(Msg.getString(this, "filters.redEye.value2")));
		this.advancedPanel.add("br", this.value2);

		this.advancedPanel.add("p", new JLabel(Msg.getString(this, "filters.redEye.maxGap")));
		this.advancedPanel.add("br", this.maxGap);

		this.add("br", this.advancedPanel);
	}

	// overwrite to not disable button
	@Override
	protected void checkButtons() {
		if (this.multipleResizableObjectPanel.getObjectVector().size() == 1 && this.multipleResizableObjectPanel.getObjectVector().get(0).isInitial()) {
			this.removeButton.setEnabled(false);
		} else {
			this.removeButton.setEnabled(true);
		}
	}

	@Override
	public void actionPerformed(final ActionEvent e) {
		super.actionPerformed(e);

		if (e.getSource() == this.showAdvanced) {
			this.advancedPanel.setVisible(this.showAdvanced.isSelected());
		} else if (e.getSource() == this.value1) {
			try {
				// parse input
				this.redEyeFilterPlugin.adjustValue1(Double.parseDouble(this.value1.getText()));
			} catch (final Exception exception) {
				this.value1.setText(Double.toString(this.redEyeFilterPlugin.getValue1()));
			}
		} else if (e.getSource() == this.value2) {
			try {
				// parse input
				this.redEyeFilterPlugin.adjustValue2(Double.parseDouble(this.value2.getText()));
			} catch (final Exception exception) {
				this.value2.setText(Double.toString(this.redEyeFilterPlugin.getValue2()));
			}
		} else if (e.getSource() == this.maxGap) {
			try {
				// parse input
				this.redEyeFilterPlugin.adjustMaxGap(Double.parseDouble(this.maxGap.getText()));
			} catch (final Exception exception) {
				this.maxGap.setText(Double.toString(this.redEyeFilterPlugin.getMaxGap()));
			}
		}
	}

	@Override
	public void adjustResizableObject(final ResizableObject resizableObject) {
		super.adjustResizableObject(resizableObject);
		this.value1.setText(Double.toString(this.redEyeFilterPlugin.getValue1()));
		this.value2.setText(Double.toString(this.redEyeFilterPlugin.getValue2()));
		this.maxGap.setText(Double.toString(this.redEyeFilterPlugin.getMaxGap()));
	}

	public void focusGained(final FocusEvent e) {
	}

	public void focusLost(final FocusEvent e) {
		if (e.getSource() == this.showAdvanced) {
			this.advancedPanel.setVisible(this.showAdvanced.isSelected());
		} else if (e.getSource() == this.value1) {
			try {
				// parse input
				this.redEyeFilterPlugin.adjustValue1(Double.parseDouble(this.value1.getText()));
			} catch (final Exception exception) {
				this.value1.setText(Double.toString(this.redEyeFilterPlugin.getValue1()));
			}
		} else if (e.getSource() == this.value2) {
			try {
				// parse input
				this.redEyeFilterPlugin.adjustValue2(Double.parseDouble(this.value2.getText()));
			} catch (final Exception exception) {
				this.value2.setText(Double.toString(this.redEyeFilterPlugin.getValue2()));
			}
		} else if (e.getSource() == this.maxGap) {
			try {
				// parse input
				this.redEyeFilterPlugin.adjustMaxGap(Double.parseDouble(this.maxGap.getText()));
			} catch (final Exception exception) {
				this.maxGap.setText(Double.toString(this.redEyeFilterPlugin.getMaxGap()));
			}
		}
	}
}
